#include "./heat_task.h"
#include "../../driver/heat/parts.h"
#include "../../driver/heat/ntc_sensor.h"

namespace task {

using namespace driver;

void HeatTask::run() {

    for (;;) {
        switch (m_state) {
            case State::IDLE: do_idle(); break;
            case State::PRE_WORK: do_pre_work(); break;
            case State::FAIL: do_stop(); break;
        }
    }
}

void HeatTask::do_idle() {
    parts->water_heat_close();
    parts->steam_heat_close();
    wait();
}

void HeatTask::do_pre_work() {

    for (;;) {
        if (!ntcSensor->is_all_ok()) {
            m_state = State::FAIL;
            break;
        }

        if (ntcSensor->water_temp() < m_heat_info.water) {
            parts->water_heat_open();
        } else {
            parts->water_heat_close();
        }

        if (ntcSensor->steam_temp() < m_heat_info.steam) {
            parts->steam_heat_open();
        } else {
            parts->steam_heat_close();
        }

        if (wait(2000)) {
            break;
        }
    }
    parts->water_heat_close();
    parts->steam_heat_close();
}

void HeatTask::do_stop() {
    parts->water_heat_close();
    parts->steam_heat_close();
    wait();
}



}
